package ServerStuff;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import DataStorageClasses.Job;
import Database.DatabaseConnector;

@WebServlet("/EditJobServlet")
public class EditJobServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	{
		DatabaseConnector db = new DatabaseConnector();
		long jobID = Long.parseLong(request.getParameter("jobID"));
		double estPrice;
		double actPrice;
		double amtPaid;
		String expStart = request.getParameter("expStartDate");
		String actStart = request.getParameter("actStartDate");
		String expFinish = request.getParameter("expFinishDate");
		String actFinish = request.getParameter("actFinishDate");
		String street = request.getParameter("street");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		Date expStartDate=null, actStartDate=null, expFinishDate=null, actFinishDate=null;
		
		try {
			try {
				estPrice = Double.parseDouble(request.getParameter("estPrice"));
				actPrice = Double.parseDouble(request.getParameter("actPrice"));
				amtPaid = Double.parseDouble(request.getParameter("amtPaid"));
				Calendar calendar = Calendar.getInstance();
				if(expStart != null) {
					String[] date = expStart.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]));
					expStartDate = new Date(calendar.getTimeInMillis());
				}
				
				if(actStart != null) {
					String[] date = actStart.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]));
					actStartDate = new Date(calendar.getTimeInMillis());
				}
				
				if(expFinish != null) {
					String[] date = expFinish.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]));
					expFinishDate = new Date(calendar.getTimeInMillis());
				}
				
				if(actFinish != null) {
					String[] date = actFinish.split("-");
					calendar.set(Integer.parseInt(date[0]), Integer.parseInt(date[1]), Integer.parseInt(date[2]));
					actFinishDate = new Date(calendar.getTimeInMillis());
				}
			}
			catch (NumberFormatException e) {
				Job job = db.getJob(jobID);
				JSONObject result = job.getJSONObject();
				result.put("failed", true);
				result.put("message", "Incorrect Date Format");
				
				response.getWriter().print(result);
				response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
				return;
			}
			catch(NullPointerException e) {
				Job job = db.getJob(jobID);
				JSONObject result = job.getJSONObject();
				result.put("failed", true);
				result.put("message", "Money Amounts Must Be Numbers");
				
				response.getWriter().print(result);
				response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
				return;
			}
			
			db.updateJob(jobID, estPrice, actPrice, amtPaid, expStartDate, actStartDate, expFinishDate, actFinishDate, street, city, province);
			Job job = db.getJob(jobID);
			JSONObject result = job.getJSONObject();
			result.put("failed", false);
			response.getWriter().print(result);
		}catch(JSONException e) { 
			e.printStackTrace();
		} catch(IOException e) {
			e.printStackTrace();
		}
		System.out.println(jobID);
		
		response.setStatus(HttpURLConnection.HTTP_ACCEPTED);
	}
}
